home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / TIME.PAK / TIMECTL.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  9KB  |  352 lines

  1. // timectl.cpp : Implementation of the CTimeCtrl OLE control class.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #ifdef __BORLANDC__
  16. #include "temptime.h"
  17. #else
  18. #include "time.h"
  19. #endif
  20. #include "timectl.h"
  21. #include "timeppg.h"
  22.  
  23.  
  24. #ifdef _DEBUG
  25. #undef THIS_FILE
  26. static char BASED_CODE THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // Numeric constants
  32.  
  33. const short ID_TIMER = 1;
  34.  
  35.  
  36. IMPLEMENT_DYNCREATE(CTimeCtrl, COleControl)
  37.  
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // Message map
  41.  
  42. BEGIN_MESSAGE_MAP(CTimeCtrl, COleControl)
  43.     //{{AFX_MSG_MAP(CTimeCtrl)
  44.     ON_WM_CREATE()
  45.     ON_WM_DESTROY()
  46.     ON_WM_ERASEBKGND()
  47.     ON_WM_TIMER()
  48.     //}}AFX_MSG_MAP
  49.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  50. END_MESSAGE_MAP()
  51.  
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // Dispatch map
  55.  
  56. BEGIN_DISPATCH_MAP(CTimeCtrl, COleControl)
  57.     //{{AFX_DISPATCH_MAP(CTimeCtrl)
  58.     DISP_PROPERTY_NOTIFY(CTimeCtrl, "Interval", m_interval, OnIntervalChanged, VT_I2)
  59.     DISP_STOCKPROP_ENABLED()
  60.     //}}AFX_DISPATCH_MAP
  61.     DISP_FUNCTION_ID(CTimeCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  62. END_DISPATCH_MAP()
  63.  
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // Event map
  67.  
  68. BEGIN_EVENT_MAP(CTimeCtrl, COleControl)
  69.     //{{AFX_EVENT_MAP(CTimeCtrl)
  70.     EVENT_CUSTOM("Timer", FireTimer, VTS_NONE)
  71.     //}}AFX_EVENT_MAP
  72. END_EVENT_MAP()
  73.  
  74.  
  75. /////////////////////////////////////////////////////////////////////////////
  76. // Property pages
  77.  
  78. // TODO: Add more property pages as needed.  Remember to increase the count!
  79. BEGIN_PROPPAGEIDS(CTimeCtrl, 1)
  80.     PROPPAGEID(CTimePropPage::guid)
  81. END_PROPPAGEIDS(CTimeCtrl)
  82.  
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // Initialize class factory and guid
  86.  
  87. IMPLEMENT_OLECREATE_EX(CTimeCtrl, "TIME.TimeCtrl.1",
  88.     0xdcf0768d, 0xba7a, 0x101a, 0xb5, 0x7a, 0x0, 0x0, 0xc0, 0xc3, 0xed, 0x5f)
  89.  
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. // Type library ID and version
  93.  
  94. IMPLEMENT_OLETYPELIB(CTimeCtrl, _tlid, _wVerMajor, _wVerMinor)
  95.  
  96.  
  97. /////////////////////////////////////////////////////////////////////////////
  98. // Interface IDs
  99.  
  100. const IID BASED_CODE IID_DTime =
  101.         { 0x37446b91, 0x5870, 0x101b, { 0xb5, 0x7b, 0x0, 0x60, 0x8c, 0xc9, 0x6a, 0xfa } };
  102. const IID BASED_CODE IID_DTimeEvents =
  103.         { 0x37446b92, 0x5870, 0x101b, { 0xb5, 0x7b, 0x0, 0x60, 0x8c, 0xc9, 0x6a, 0xfa } };
  104.  
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107. // Control type information
  108.  
  109. static const DWORD BASED_CODE _dwTimeOleMisc =
  110.     OLEMISC_INVISIBLEATRUNTIME |
  111.     OLEMISC_ACTIVATEWHENVISIBLE |
  112.     OLEMISC_SETCLIENTSITEFIRST |
  113.     OLEMISC_INSIDEOUT |
  114.     OLEMISC_CANTLINKINSIDE |
  115.     OLEMISC_RECOMPOSEONRESIZE;
  116.  
  117. IMPLEMENT_OLECTLTYPE(CTimeCtrl, IDS_TIME, _dwTimeOleMisc)
  118.  
  119.  
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CTimeCtrl::CTimeCtrlFactory::UpdateRegistry -
  122. // Adds or removes system registry entries for CTimeCtrl
  123.  
  124. BOOL CTimeCtrl::CTimeCtrlFactory::UpdateRegistry(BOOL bRegister)
  125. {
  126.     if (bRegister)
  127.         return AfxOleRegisterControlClass(
  128.             AfxGetInstanceHandle(),
  129.             m_clsid,
  130.             m_lpszProgID,
  131.             IDS_TIME,
  132.             IDB_TIME,
  133.             FALSE,                      //  Not insertable
  134.             _dwTimeOleMisc,
  135.             _tlid,
  136.             _wVerMajor,
  137.             _wVerMinor);
  138.     else
  139.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  140. }
  141.  
  142.  
  143. /////////////////////////////////////////////////////////////////////////////
  144. // CTimeCtrl::CTimeCtrl - Constructor
  145.  
  146. CTimeCtrl::CTimeCtrl()
  147. {
  148.     InitializeIIDs(&IID_DTime, &IID_DTimeEvents);
  149.  
  150.     SetInitialSize(24, 22);
  151.     m_interval = (short) DEFAULT_INTERVAL; // initialize interval property
  152. }
  153.  
  154.  
  155. /////////////////////////////////////////////////////////////////////////////
  156. // CTimeCtrl::~CTimeCtrl - Destructor
  157.  
  158. CTimeCtrl::~CTimeCtrl()
  159. {
  160. }
  161.  
  162.  
  163. /////////////////////////////////////////////////////////////////////////////
  164. // CTimeCtrl::OnDraw - Drawing function
  165.  
  166. void CTimeCtrl::OnDraw(
  167.             CDC* pdc, const CRect& rcBounds, const CRect&)
  168. {
  169.     CBitmap bitmap;
  170.     BITMAP  bmp;
  171.     CPictureHolder picHolder;
  172.     CRect rcSrcBounds;
  173.  
  174.     // Load clock bitmap
  175.     bitmap.LoadBitmap(IDB_CLOCK);
  176.     bitmap.GetObject(sizeof(BITMAP), &bmp);
  177.     rcSrcBounds.right = bmp.bmWidth;
  178.     rcSrcBounds.bottom = bmp.bmHeight;
  179.  
  180.     // Create picture and render
  181.     picHolder.CreateFromBitmap((HBITMAP)bitmap.m_hObject, NULL, FALSE);
  182.     picHolder.Render(pdc, rcBounds, rcSrcBounds);
  183. }
  184.  
  185.  
  186. /////////////////////////////////////////////////////////////////////////////
  187. // CTimeCtrl::DoPropExchange - Persistence support
  188.  
  189. void CTimeCtrl::DoPropExchange(CPropExchange* pPX)
  190. {
  191.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  192.     COleControl::DoPropExchange(pPX);
  193.  
  194.     short nInterval = m_interval;
  195.     PX_Short(pPX, _T("Interval"), (short) m_interval, (short) DEFAULT_INTERVAL);
  196.  
  197.     if (pPX->IsLoading())
  198.     {
  199.         if (nInterval != m_interval)
  200.             OnIntervalChanged();    // Force timer to recreate w/ correct interval
  201.     }
  202. }
  203.  
  204.  
  205. /////////////////////////////////////////////////////////////////////////////
  206. // CTimeCtrl::OnResetState - Reset control to default state
  207.  
  208. void CTimeCtrl::OnResetState()
  209. {
  210.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  211. }
  212.  
  213.  
  214. /////////////////////////////////////////////////////////////////////////////
  215. // CTimeCtrl::AboutBox - Display an "About" box to the user
  216.  
  217. void CTimeCtrl::AboutBox()
  218. {
  219.     CDialog dlgAbout(IDD_ABOUTBOX_TIME);
  220.     dlgAbout.DoModal();
  221. }
  222.  
  223.  
  224. /////////////////////////////////////////////////////////////////////////////
  225. // CTimeCtrl::OnEnabledChanged - Start/stop the timer when the enable state
  226. // has changed.
  227.  
  228. void CTimeCtrl::OnEnabledChanged()
  229. {
  230.     if (AmbientUserMode())
  231.     {
  232.         if (GetEnabled())
  233.         {
  234.             if (GetHwnd() != NULL)
  235.                 StartTimer();
  236.         }
  237.         else
  238.             StopTimer();
  239.     }
  240. }
  241.  
  242.  
  243. /////////////////////////////////////////////////////////////////////////////
  244. // CTimeCtrl::OnIntervalChanged - Respond to a change of interval.
  245.  
  246. void CTimeCtrl::OnIntervalChanged()
  247. {
  248.     // Restart timer if it's enabled
  249.     if (GetEnabled() && AmbientUserMode())
  250.     {
  251.         StopTimer();
  252.         StartTimer();
  253.     }
  254.  
  255.     SetModifiedFlag(TRUE);
  256. }
  257.  
  258.  
  259. /////////////////////////////////////////////////////////////////////////////
  260. // CTimeCtrl::OnSetClientSite - Force creation of window
  261.  
  262. void CTimeCtrl::OnSetClientSite()
  263. {
  264.     RecreateControlWindow();
  265. }
  266.  
  267.  
  268. /////////////////////////////////////////////////////////////////////////////
  269. // CTimeCtrl::OnCreate - Start the timer if it's enabled.
  270.  
  271. int CTimeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
  272. {
  273.     if (COleControl::OnCreate(lpCreateStruct) == -1)
  274.         return -1;
  275.  
  276.     // Start timer if in user mode and if enabled
  277.     if (AmbientUserMode() && GetEnabled())
  278.         StartTimer();
  279.  
  280.     return 0;
  281. }
  282.  
  283.  
  284. /////////////////////////////////////////////////////////////////////////////
  285. // CTimeCtrl::OnDestroy - Stop the timer before the control is destroyed.
  286.  
  287. void CTimeCtrl::OnDestroy()
  288. {
  289.     StopTimer();
  290.  
  291.     COleControl::OnDestroy();
  292. }
  293.  
  294.  
  295. /////////////////////////////////////////////////////////////////////////////
  296. // CTimeCtrl::OnEraseBkgnd - Simply return TRUE so background is not erased.
  297.  
  298. BOOL CTimeCtrl::OnEraseBkgnd(CDC*)
  299. {
  300.     return TRUE;
  301. }
  302.  
  303.  
  304. /////////////////////////////////////////////////////////////////////////////
  305. // CTimeCtrl::OnTimer - Fire the timer event.
  306.  
  307. void CTimeCtrl::OnTimer(UINT)
  308. {
  309.     FireTimer();
  310. }
  311.  
  312.  
  313. /////////////////////////////////////////////////////////////////////////////
  314. // CTimeCtrl::OnAmbientPropertyChange - Start the timer if user mode ambient
  315. // property has changed and if timer is enabled.  Stop the timer if user
  316. // mode ambient property has changed and if timer is enabled.
  317.  
  318. void CTimeCtrl::OnAmbientPropertyChange(DISPID dispid)
  319. {
  320.     if (dispid == DISPID_AMBIENT_USERMODE)
  321.     {
  322.         // Start or stop the timer
  323.         if (GetEnabled())
  324.         {
  325.             if (AmbientUserMode())
  326.                 StartTimer();
  327.             else
  328.                 StopTimer();
  329.         }
  330.  
  331.         InvalidateControl();
  332.     }
  333. }
  334.  
  335.  
  336. /////////////////////////////////////////////////////////////////////////////
  337. // CTimeCtrl::StartTimer - Start the timer.
  338.  
  339. void CTimeCtrl::StartTimer()
  340. {
  341.     SetTimer(ID_TIMER, m_interval, NULL);
  342. }
  343.  
  344.  
  345. /////////////////////////////////////////////////////////////////////////////
  346. // CTimeCtrl::StopTimer - Stop the timer.
  347.  
  348. void CTimeCtrl::StopTimer()
  349. {
  350.     KillTimer(ID_TIMER);
  351. }
  352.